package com.voistech.sbccoder;

import android.util.Log;

public class SBCCodec {
    private static final String TAG = "SBCCodec";
    /* sampling frequency */
    static final public int SBC_FREQ_16000 = 0x00;
    static final public int SBC_FREQ_32000 = 0x01;
    static final public int SBC_FREQ_44100 = 0x02;
    static final public int SBC_FREQ_48000 = 0x03;

    /* blocks */
    static final public int SBC_BLK_4 = 0x00;
    static final public int SBC_BLK_8 = 0x01;
    static final public int SBC_BLK_12 = 0x02;
    static final public int SBC_BLK_16 = 0x03;

    /* channel mode */
    static final public int SBC_MODE_MONO = 0x00;
    static final public int SBC_MODE_DUAL_CHANNEL = 0x01;
    static final public int SBC_MODE_STEREO = 0x02;
    static final public int SBC_MODE_JOINT_STEREO = 0x03;

    /* allocation method */
    static final public int SBC_AM_LOUDNESS = 0x00;
    static final public int SBC_AM_SNR = 0x01;

    /* subbands */
    static final public int SBC_SB_4 = 0x00;
    static final public int SBC_SB_8 = 0x01;

    static {
        System.loadLibrary("sbccodec");
    }

    private long sbcHandle;

    public boolean init(int sampleRate, int blocks, int channelMode,
                     int allocMethod, int subBands, int bitpool) {
        return sbcInit(sampleRate, blocks, channelMode, allocMethod, subBands, bitpool);
    }

    public boolean deInit() {
        return sbcDeInit();
    }

    public int encode(short[] input, int inputLen, byte[] output, int outputLen, int[] written) {
        return sbcEncode(input, inputLen, output, outputLen, written);
    }

    public int decode(byte[] input, int inputLen, short[] output, int outputLen, int[] len) {
        return sbcDecode(input, inputLen, output, outputLen, len);
    }

    public int getFrameLength() {
        return sbcGetFrameLength();
    }

    public int getFrameDuration() {
        return sbcGetFrameDuration();
    }

    public int getCodeSize() {
        return sbcGetCodeSize();
    }

    public void finishSbc() {
        sbcFinish();
    }

    private native boolean sbcInit(int sampleRate, int blocks,
                                int channelMode, int allocMethod,
                                int subbands, int bitpool);
    private native boolean sbcDeInit();
    private native int sbcEncode(short[] input,
                                 int inputLen, byte[] output,
                                 int outputLen, int[] written);
    private native int sbcDecode(byte[] input,
                                 int inputLen, short[] output,
                                 int outputLen, int[] len);
    private native int sbcGetFrameLength();
    private native int sbcGetFrameDuration();
    private native int sbcGetCodeSize();
    private native void sbcFinish();
}
